package com.evan.wj.interceptor;

import com.evan.wj.pojo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//测试与git代码版本差异---
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletrequest, HttpServletResponse httpServletresponse, Object o) throws Exception {
        HttpSession session = httpServletrequest.getSession();
        String contextPath = session.getServletContext().getContextPath();
        String[] requireAuthPages = new String[]{"index"};

        String uri = httpServletrequest.getRequestURI();

        uri = StringUtils.remove(uri, contextPath + "/");

        String page = uri;

        if (beginWith(page, requireAuthPages)) {
            User user = (User) session.getAttribute("user");
            if (null == user) {
                httpServletresponse.sendRedirect("login");
                return false;
            }
        }
        return true;
    }

    private boolean beginWith(String page, String[] requireAuthPages) {
        boolean result = false;
        for (String requireAuthPage : requireAuthPages) {
            if (StringUtils.startsWith(page, requireAuthPage)) {
                result = true;
                break;
            }
        }
        return result;
    }
}
